<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Common\ajaxReturnJsonContent;
use App\Http\Requests\UsersUpdateFormCheck;
use App\Http\Requests\UsersAddFormCheck;
use App\User;
use App\Role;
use DB;

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::with('roles.perms')->get();
        $Role = Role::get();
        return View('admin.users', ['users' => $users, 'RoleAll' => $Role]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(UsersAddFormCheck $request)
    {
        if ($request->password != $request->repassword) {
            return back()->with(['waring', '两次密码不一致']);
        }


        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => password_hash($request->password, PASSWORD_DEFAULT)
        ]);
        if ($request->roles) {
            $user->roles()->sync($request->roles);
        }
        return back()->with(['waring', '添加成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $name)
    {
        $res = User::where($request->field, $name)->first();
        $result = ajaxReturnJsonContent::returnVal($res, 'success', 'fail');
        return $result;
    }

    /**
     * 前台ajax获取单条数据.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = User::with('roles.perms')->where('id', $id)->first();
        return $data;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UsersUpdateFormCheck $request, $id)
    {
        $res = [];
        //判断是否有密码
        if ($request->password) {
            //有密码则判断两次密码是否一致
            if ($request->password == $request->repassword) {
                $res['password'] = password_hash($request->password, PASSWORD_DEFAULT);
            } else {
                return redirect('admin/Users')->with('两次密码不一致');
            }
        }
        $res['name'] = $request->name;
        $res['email'] = $request->email;


        $user = User::find($id);

        if (!$user) {
            return redirect('admin/Users')->with(['waring' => '用户不存在']);
        }

        $user->fill($res)->save();

        if ($roleArr = $request->roles) {
            $user->roles()->sync($roleArr);
        } else {
            $user->roles()->detach();
        }

        return redirect('admin/Users')->with(['waring' => '修改成功']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $user = User::find($id);
        if ($user) {
            $user->delete();
        } else {
            return redirect('admin/Users')->with(['waring' => '删除失败，用户不存在']);
        }
        return redirect('admin/Users')->with(['waring' => '删除成功']);
    }
}
